###################################################################################
#
# Code for replicating Figures 2, 3 and 4 in: 
#  
#  Huijsmans, T. & Rodden, J. (2024) The Great Global Divider? A Comparison of 
#   Urban-Rural Partisan Polarization in Western Democracies. Comparative Political
#   Studies.
#
# Steps:
#  
#   1. 	Download URED_elections.csv and URED_parties.csv from:
#       https://osf.io/gwumr/files/osfstorage
#
#   2. 	Save the datasets in the same folder and write the location of this folder 
#       in the setwd command below

  # Set working directory, use same folder as in the replication .do-file:
  setwd("........................")
#
#   3.  Run the code below
###################################################################################




library(haven)
library(tidyverse)

library(ggplot2)
library(dplyr)
library(hrbrthemes)

library(ggpubr)

data <- read_dta("URED_parties.dta")


mybreaks <- c(0.05, 0.1, 0.15, 0.2, 0.25, 0.4, 0.5, 0.6)


### Netherlands
data_NL <- data %>% subset(country_code == 'NL' & 
                             (party_abbreviation == 'GL' | party_abbreviation == 'PVV' |
                                party_abbreviation == 'PVDA' | party_abbreviation == 'VVD' |
                                party_abbreviation == 'CDA' | party_abbreviation == 'SP' |
                                party_abbreviation == 'PVDD' | party_abbreviation == 'D66'|
                                party_abbreviation == 'CU' | party_abbreviation == 'SGP' |
                                party_abbreviation == 'BBB' | party_abbreviation == 'NSC' |
                                party_abbreviation == 'GLPvdA'))

summary(data_NL$nat_voteshare)

myrange <- c(1,7)

# Plot with color legend
p_NL <- data_NL %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NL,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('#CCFF00', 'springgreen4', 'steelblue', 'purple', 'limegreen', '#993300', '#9999FF', 'rosybrown', 'blue',
                                            'grey1', 'lightcoral', 'red', 'orange' )) +
  ggtitle("Netherlands") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=8)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_NL)

# Plot with size legend
p_NL2 <- data_NL %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NL,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('#CCFF00', 'springgreen4', 'steelblue', 'purple', 'limegreen', '#993300', '#9999FF', 'rosybrown', 'blue',
                              'grey1', 'lightcoral', 'red', 'orange' )) +
  ggtitle("Netherlands") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_NL2)

# Plot without legend
p_NL_nol <- data_NL %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NL,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('#CCFF00', 'springgreen4', 'steelblue', 'purple', 'limegreen', '#993300', '#9999FF', 'rosybrown', 'blue',
                              'grey1', 'lightcoral', 'red', 'orange' )) +
  ggtitle("Netherlands") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"),
          plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55) +
  labs(x = "", y = "") 
  



# Arrange plot and both legends
P_NL_l1 <- cowplot::plot_grid(p_NL_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_NL_l12 <- cowplot::plot_grid(P_NL_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1))




### Germany
data_GE <- data %>% subset(country_code == 'GE' & 
                             (party_abbreviation == 'CDU' | party_abbreviation == 'SPD' |
                                party_abbreviation == 'FDP' | party_abbreviation == 'GREENS' |
                                party_abbreviation == 'AFD' | party_abbreviation == 'LINKE'))
data_GE <- data_GE %>% 
  mutate(party_abbreviation = ifelse(party_abbreviation=='GREENS', 'GR', party_abbreviation) ) %>%
  mutate(party_abbreviation = ifelse(party_abbreviation=='LINKE', 'LI', party_abbreviation) )

summary(data_GE$nat_voteshare)
myrange <- c(1,10.5)

# Plot with color legend
p_GE <- data_GE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_GE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c( 'grey1', 'springgreen4', 'purple', 'limegreen','red', 'rosybrown' )) +
  ggtitle("Germany") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=6, title=element_blank()),
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_GE)


# Plot with size legend
p_GE2 <- data_GE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_GE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c( 'grey1', 'springgreen4', 'purple', 'limegreen','red', 'rosybrown' )) +
  ggtitle("Germany") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_GE2)

# Plot without legend
p_GE_nol <- data_GE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_GE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c( 'grey1', 'springgreen4', 'purple', 'limegreen','red', 'rosybrown' )) +
  ggtitle("Germany") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"),
          plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

# Arrange plot and both legends
P_GE_l1 <- cowplot::plot_grid(p_GE_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_GE_l12 <- cowplot::plot_grid(P_GE_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 


### Norway
data_NO <- data %>% subset(country_code == 'NO' & 
                             (party_abbreviation == 'DNA' | party_abbreviation == 'FRP' |
                                party_abbreviation == 'H' | party_abbreviation == 'KRF' |
                                party_abbreviation == 'MDG' | party_abbreviation == 'RV'
                              | party_abbreviation == 'SP' | party_abbreviation == 'SV'
                              | party_abbreviation == 'V')) 
                                                        
table(data_NO$party_abbreviation, data_NO$year)

# note: recode extreme values of SP  to -.6 to fit in the graph

data_NO <- data_NO %>% mutate(ur_pol_dir4 = ifelse(party_abbreviation=='SP', -.6, ur_pol_dir4) ) 
  
  

summary(data_NO$nat_voteshare)
myrange <- c(1,8.5)

# Plot with color legend
p_NO <- data_NO %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NO,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('rosybrown', 'grey1', 'orange',  'springgreen4', 'limegreen','blue','gold', 'red', 'purple' )) +
  ggtitle("Norway") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=6)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)  + 
    annotate("text",  
           x = c(1977),
           y = c(-0.55),
           label = c("*"),
           color="black", size=8 , angle=320) 

legend1 <- cowplot::get_legend(p_NO)

# Plot with size legend

p_NO2 <- data_NO %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NO,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('rosybrown', 'grey1', 'orange',  'springgreen4', 'limegreen','blue','gold', 'red', 'purple' )) +
  ggtitle("Norway") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)  + 
  annotate("text",  
           x = c(1977),
           y = c(-0.55),
           label = c("*"),
           color="black", size=8 , angle=320) 

legend2 <- cowplot::get_legend(p_NO2)

# Plot without legend

p_NO_nol <- data_NO %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NO,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('rosybrown', 'grey1', 'orange',  'springgreen4', 'limegreen','blue','gold', 'red', 'purple' )) +
  ggtitle("Norway") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)  + 
  annotate("text",  
           x = c(1977),
           y = c(-0.55),
           label = c("*"),
           color="black", size=8 , angle=320) 


# Arrange plot and both legends
P_NO_l1 <- cowplot::plot_grid(p_NO_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_NO_l12 <- cowplot::plot_grid(P_NO_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Sweden
data_SE <- data %>% subset(country_code == 'SE' & party_abbreviation != 'oth') 
data_SE <- data_SE %>% 
  mutate(party_abbreviation = ifelse(party_abbreviation=='GREENS', 'GR', party_abbreviation) )

table(data_SE$party_abbreviation, data_SE$year)

summary(data_SE$nat_voteshare)
myrange <- c(1,9)

# Plot with color legend
p_SE <- data_SE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_SE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=c(0,8), breaks=mybreaks) +
  scale_color_manual(values=c('springgreen4', 'purple', 'limegreen','orange','red', 'blue', 'grey1', 'rosybrown' )) +
  ggtitle("Sweden") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_SE)

# Plot with size legend
p_SE2 <- data_SE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_SE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=c(0,8), breaks=mybreaks) +
  scale_color_manual(values=c('springgreen4', 'purple', 'limegreen','orange','red', 'blue', 'grey1', 'rosybrown' )) +
  ggtitle("Sweden") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_SE2)

# Plot without legend
p_SE_nol <- data_SE %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_SE,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=c(0,8), breaks=mybreaks) +
  scale_color_manual(values=c('springgreen4', 'purple', 'limegreen','orange','red', 'blue', 'grey1', 'rosybrown' )) +
  ggtitle("Sweden") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)



# Arrange plot and both legends
P_SE_l1 <- cowplot::plot_grid(p_SE_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_SE_l12 <- cowplot::plot_grid(P_SE_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Switzerland
data_CH <- data %>% subset(country_code == 'CH' & (party_abbreviation=='BDP' 
                           | party_abbreviation=='CVP' | party_abbreviation=='EDU' 
                           | party_abbreviation=='EVP' | party_abbreviation=='FDP'
                           | party_abbreviation=='GLP' | party_abbreviation=='GPS'
                           | party_abbreviation=='SP' | party_abbreviation=='SVP'
                           | party_abbreviation=='SD' | party_abbreviation=='Mitte')) 
table(data_CH$party_abbreviation, data_CH$year)

summary(data_CH$nat_voteshare)
myrange <- c(1,6.5)

# Plot with color legend
p_CH <- data_CH %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CH,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'rosybrown', 'purple', 'orange', 'steelblue', 'gold',
                                            'limegreen', "cyan", 'grey1', 'red', 'springgreen4' )) +
  ggtitle("Switzerland") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=7)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_CH)

# Plot with size legend
p_CH2 <- data_CH %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CH,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'rosybrown', 'purple', 'orange', 'steelblue', 'gold',
                              'limegreen', "cyan", 'grey1', 'red', 'springgreen4' )) +
  ggtitle("Switzerland") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


legend2 <- cowplot::get_legend(p_CH2)

# Plot without legend
p_CH_nol <- data_CH %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CH,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'rosybrown', 'purple', 'orange', 'steelblue', 'gold',
                              'limegreen', "cyan", 'grey1', 'red', 'springgreen4' )) +
  ggtitle("Switzerland") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

# Arrange plot and both legends
P_CH_l1 <- cowplot::plot_grid(p_CH_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_CH_l12 <- cowplot::plot_grid(P_CH_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 


### France
data_FR <- data %>% subset(country_code == 'FR' & (party_abbreviation=='COM' 
                                                   | party_abbreviation=='ECO' | party_abbreviation=='REC' 
                                                   | party_abbreviation=='SOC' | party_abbreviation=='RN'
                                                   | party_abbreviation=='LR' | party_abbreviation=='DVD'
                                                   | party_abbreviation=='LREM/ENS' | party_abbreviation=='NUP'
                                                   | party_abbreviation=='EXG' | party_abbreviation=='DVG')) 


data_FR <- data_FR %>% mutate(party_abbreviation = ifelse(party_abbreviation=='LREM/ENS', 'LREM', party_abbreviation) )

table(data_FR$party_abbreviation, data_FR$year)
summary(data_FR$nat_voteshare)
myrange <- c(0,8)


# Plot with color legend
p_FR <- data_FR %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_FR,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('red', 'cyan1', 'purple', 'limegreen', 'steelblue', 'orange', 'gold',
                                    'springgreen4', 'blue', 'grey1', 'lightcoral' )) +
                                      ggtitle("France") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=7)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_FR)

# Plot with size legend
p_FR2 <- data_FR %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_FR,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('red', 'cyan1', 'purple', 'limegreen', 'steelblue', 'orange', 'gold',
                              'springgreen4', 'blue', 'grey1', 'lightcoral' )) +
  ggtitle("France") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_FR2)

# Plot without legend
p_FR_nol <- data_FR %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_FR,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('red', 'cyan1', 'purple', 'limegreen', 'steelblue', 'orange', 'gold',
                              'springgreen4', 'blue', 'grey1', 'lightcoral' )) +
  ggtitle("France") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_FR_l1 <- cowplot::plot_grid(p_FR_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_FR_l12 <- cowplot::plot_grid(P_FR_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Spain

data_ES <- data %>% subset(country_code == 'ES' & (party_abbreviation=='PP' 
                                                   | party_abbreviation=='PODEMOS' | party_abbreviation=='CDC' 
                                                   | party_abbreviation=='PSOE' | party_abbreviation=='IU'
                                                   | party_abbreviation=='Cs' | party_abbreviation=='VOX') )

data_ES <- data_ES %>% mutate(party_abbreviation = ifelse(party_abbreviation=='PODEMOS', 'POD', party_abbreviation) )

table(data_ES$party_abbreviation, data_ES$year)

summary(data_ES$nat_voteshare)
myrange <- c(1,9.5)

# Plot with color legend
p_ES <- data_ES %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_ES,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('purple', 'lightcoral','springgreen4', 'orange', 'blue', 'red', 'grey1' )) +
  ggtitle("Spain") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_ES)



# Plot with size legend
p_ES2 <- data_ES %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_ES,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('purple', 'lightcoral','springgreen4', 'orange', 'blue', 'red', 'grey1' )) +
  ggtitle("Spain") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
         size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


legend2 <- cowplot::get_legend(p_ES2)

# Plot without legend
p_ES_nol <- data_ES %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_ES,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('purple', 'lightcoral','springgreen4', 'orange', 'blue', 'red', 'grey1' )) +
  ggtitle("Spain") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_ES_l1 <- cowplot::plot_grid(p_ES_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_ES_l12 <- cowplot::plot_grid(P_ES_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 


### Italy

data_IT <- data %>% subset(country_code == 'IT' & (party_abbreviation=='AN' | party_abbreviation=='M5S'
                                                   | party_abbreviation=='DC' | party_abbreviation=='FDI' 
                                                   | party_abbreviation=='FORZIT' | party_abbreviation=='LEGA'
                                                   | party_abbreviation=='PCI'
                                                   | party_abbreviation=='PD' | party_abbreviation=='PDS'
                                                   | (party_abbreviation=='PSI' & year<1994) | party_abbreviation=='ULIVO' |
                                                     party_abbreviation=='A_IV_C'))

data_IT <- data_IT %>% mutate(party_abbreviation = ifelse(party_abbreviation=='FORZIT', 'FOR', party_abbreviation),
                              party_abbreviation = ifelse(party_abbreviation=='A_IV_C', 'A_IV', party_abbreviation))

table(data_IT$party_abbreviation, data_IT$year)

summary(data_IT$nat_voteshare)
myrange <- c(1,7.5)

# Plot with color legend
p_IT <- data_IT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_IT,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c("#D55E00", 'grey1', 'orange', 'red', 'limegreen', 'steelblue', 'lightcoral', 'gold',
                              'springgreen4', 'blue', 'purple', 'cyan1' )) +
  ggtitle("Italy") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=7)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_IT)

# Plot with size legend
p_IT2 <- data_IT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_IT,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c("#D55E00", 'grey1', 'orange', 'red', 'limegreen', 'steelblue', 'lightcoral', 'gold',
                              'springgreen4', 'blue', 'purple', 'cyan1' )) +
  ggtitle("Italy") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
         size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_IT2)



# Plot without legend
p_IT_nol <- data_IT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_IT,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c("#D55E00", 'grey1', 'orange', 'red', 'limegreen', 'steelblue', 'lightcoral', 'gold',
                              'springgreen4', 'blue', 'purple', 'cyan1' )) +
  ggtitle("Italy") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_IT_l1 <- cowplot::plot_grid(p_IT_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_IT_l12 <- cowplot::plot_grid(P_IT_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 


### United States

mybreaksUS <- c(0.4,0.43, 0.45, 0.48, 0.5, 0.53, 0.55, 0.6)


data_US <- data %>% subset(country_code == 'US_cong' & party_abbreviation!='OTH')

table(data_US$party_abbreviation, data_US$year)

summary(data_US$nat_voteshare)
myrange <- c(5,12)

# Plot with color legend
p_US <- data_US %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_US,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaksUS) +
  scale_color_manual(values=c('blue', 'red' )) +
  ggtitle("United States") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_US)

# Plot with size legend
p_US2 <- data_US %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_US,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaksUS) +
  scale_color_manual(values=c('blue', 'red' )) +
  ggtitle("United States") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_US2)



# Plot without legend
p_US_nol <- data_US %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_US,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'red' )) +
  ggtitle("United States") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_US_l1 <- cowplot::plot_grid(p_US_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_US_l12 <- cowplot::plot_grid(P_US_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Canada

data_CA <- data %>% subset(country_code == 'CA' & (party_abbreviation=='BlQu' | party_abbreviation=='CON'
                                                   | party_abbreviation=='Green' | party_abbreviation=='LIB' 
                                                   | party_abbreviation=='NewDem' ))
data_CA <- data_CA %>% 
  mutate(party_abbreviation = ifelse(party_abbreviation=='Green', 'GR', party_abbreviation) ) %>%
  mutate(party_abbreviation = ifelse(party_abbreviation=='NewDem', 'ND', party_abbreviation) )

table(data_CA$party_abbreviation, data_CA$year)

summary(data_CA$nat_voteshare)
myrange<-c(1,10)

# Plot with color legend
p_CA <- data_CA %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CA,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('grey1', 'red', 'springgreen4', 'blue','orange')) +
  ggtitle("Canada") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_CA)


# Plot with size legend
p_CA2 <- data_CA %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CA,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('grey1', 'red', 'springgreen4', 'blue','orange')) +
  ggtitle("Canada") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_CA2)

# Plot without legend
p_CA_nol <- data_CA %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_CA,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('grey1', 'red', 'springgreen4', 'blue','orange')) +
  ggtitle("Canada") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_CA_l1 <- cowplot::plot_grid(p_CA_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_CA_l12 <- cowplot::plot_grid(P_CA_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 



### United Kingdom

data_UK <- data %>% subset(country_code == 'UK' & (party_abbreviation=='con' | party_abbreviation=='green'
                                                   | party_abbreviation=='lab' | party_abbreviation=='lib' 
                                                   | party_abbreviation=='ukip' ) & year>1990)

table(data_UK$party_abbreviation, data_UK$year)
data_UK <- data_UK %>% mutate(party_abbreviation = ifelse(party_abbreviation=='green', 'gr', party_abbreviation) )

summary(data_UK$nat_voteshare)
myrange<-c(1,9)

# Plot with color legend
p_UK <- data_UK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_UK,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'springgreen4', 'red', 'orange','grey1')) +
  ggtitle("United Kingdom") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=6)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


legend1 <- cowplot::get_legend(p_UK)


# Plot with size legend
p_UK2 <- data_UK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_UK,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'springgreen4', 'red', 'orange','grey1')) +
  ggtitle("United Kingdom") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
         size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_UK2)



# Plot without legend
p_UK_nol <- data_UK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_UK,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'springgreen4', 'red', 'orange','grey1')) +
  ggtitle("United Kingdom") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_UK_l1 <- cowplot::plot_grid(p_UK_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_UK_l12 <- cowplot::plot_grid(P_UK_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Australia

data_AUS <- data %>% subset(country_code == 'AUS' & (party_abbreviation=='LAB' | party_abbreviation=='LP'
                                                     | party_abbreviation=='NAT' | party_abbreviation=='LNPQ'
                                                    | party_abbreviation=='Greens' | party_abbreviation=='DEM' ))
data_AUS <- data_AUS %>% mutate(party_abbreviation = ifelse(party_abbreviation=='Greens', 'GR', party_abbreviation) )

# note: recode extreme values of NAT party to -.6 to fit in the graph

data_AUS <- data_AUS %>% mutate(ur_pol_dir4 = ifelse(party_abbreviation=='NAT', -.6, ur_pol_dir4) )

table(data_AUS$party_abbreviation, data_AUS$year)

summary(data_AUS$nat_voteshare)
myrange<-c(1,10)

# Plot with color legend
p_AUS <- data_AUS %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AUS,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('orange', 'limegreen', 'red', 'purple', 'blue', 'springgreen4')) +
  ggtitle("Australia") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25),
        plot.margin = unit(c(0,0,0,0), "lines")) +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55) +
  annotate("text",  
           x = c(1972),
           y = c(-.55),
           label = c("*"),
           color="black", size=8) 

legend1 <- cowplot::get_legend(p_AUS)

# Plot with size legend
p_AUS2 <- data_AUS %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AUS,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('orange', 'limegreen', 'red', 'purple', 'blue', 'springgreen4')) +
  ggtitle("Australia") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_AUS2)



# Plot without legend
p_AUS_nol <- data_AUS %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AUS,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks) +
  scale_color_manual(values=c('orange', 'limegreen', 'red', 'purple', 'blue', 'springgreen4')) +
  ggtitle("Australia") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_AUS_l1 <- cowplot::plot_grid(p_AUS_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_AUS_l12 <- cowplot::plot_grid(P_AUS_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 


### New Zealand
data_NZ <- data %>% subset(country_code == 'NZ' & 
                             (party_abbreviation == 'ACT' | party_abbreviation == 'alliance' |
                                party_abbreviation == 'Green' | party_abbreviation == 'National' |
                                party_abbreviation == 'Cons' | party_abbreviation == 'Labour'| 
                                party_abbreviation == 'NZfirst' ))
summary(data_NZ$nat_voteshare)
myrange<-c(1,10)

# Plot with color legend
p_NZ <- data_NZ %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NZ,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'orange', 'limegreen', 'red', 'blue', 'rosybrown')) +
  ggtitle("New Zealand") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


legend1 <- cowplot::get_legend(p_NZ)


# Plot with size legend
p_NZ2 <- data_NZ %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NZ,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'orange', 'limegreen', 'red', 'blue', 'rosybrown')) +
  ggtitle("New Zealand") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_NZ2)



# Plot without legend
p_NZ_nol <- data_NZ %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_NZ,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'orange', 'limegreen', 'red', 'blue', 'rosybrown')) +
  ggtitle("New Zealand") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_NZ_l1 <- cowplot::plot_grid(p_NZ_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_NZ_l12 <- cowplot::plot_grid(P_NZ_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Austria

data_AT <- data %>% subset(country_code == 'AT' & 
                             (party_abbreviation == 'BZÖ' | party_abbreviation == 'FPÖ' |
                                party_abbreviation == 'GRÜNE' | party_abbreviation == 'NEOS' |
                                party_abbreviation == 'SPÖ' | party_abbreviation == 'ÖVP'| 
                                party_abbreviation == 'LIF' ))
summary(data_AT$nat_voteshare)
myrange<-c(1,9)

# Plot with color legend
p_AT <- data_AT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AT,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'limegreen', 'orange','rosybrown' , 'blue', 'red')) +
  ggtitle("Austria") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


legend1 <- cowplot::get_legend(p_AT)


# Plot with size legend
p_AT2 <- data_AT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AT,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'limegreen', 'orange','rosybrown' , 'blue', 'red')) +
  ggtitle("Austria") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
         size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_AT2)

# Plot without legend
p_AT_nol <- data_AT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AT,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=myrange, breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'limegreen', 'orange','rosybrown' , 'blue', 'red')) +
  ggtitle("Austria") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_AT_l1 <- cowplot::plot_grid(p_AT_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_AT_l12 <- cowplot::plot_grid(P_AT_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 

### Denmark

data_DK <- data %>% subset(country_code == 'DK' & 
                             (party_abbreviation == 'ALT' | party_abbreviation == 'CON' |
                                party_abbreviation == 'DD' | party_abbreviation == 'DPP' |
                                party_abbreviation == 'LA' | party_abbreviation == 'MOD' |
                                party_abbreviation == 'RGA'|
                                party_abbreviation == 'SDP' | party_abbreviation == 'RV' |
                                party_abbreviation == 'SOC' | party_abbreviation == 'VDLP' |
                                party_abbreviation == 'FKP' | party_abbreviation == 'CentD')) %>%
  mutate(party_abbreviation = ifelse(party_abbreviation == 'SOC',  'SF', party_abbreviation))

summary(data_DK$nat_voteshare)
myrange<-c(1,5)


# Plot with color legend
p_DK <- data_DK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_DK,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  scale_color_manual(values=c('cyan3', 'steelblue', 'lightcoral', 'grey1', 'purple', 'blue',
                              'rosybrown', 'limegreen', 'red', 'darkgoldenrod', 'orange',  'darkgreen',
                               'grey65')) +
  ggtitle("Denmark") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=8)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend1 <- cowplot::get_legend(p_DK)


# Plot with size legend
p_DK2 <- data_DK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_DK,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  scale_color_manual(values=c('cyan3', 'steelblue', 'lightcoral', 'grey1', 'purple', 'blue',
                              'rosybrown', 'limegreen', 'red', 'darkgoldenrod', 'orange',  'darkgreen',
                              'grey65')) +
  ggtitle("Denmark") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
         size =guide_legend(ncol=1, position="bottom", title.position = "top"))  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "right", legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        plot.margin = unit(c(0,0,0,0), "lines"),
        legend.title =element_text(size=10))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)

legend2 <- cowplot::get_legend(p_DK2)



# Plot without legend
p_DK_nol <- data_DK %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_DK,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  scale_color_manual(values=c('cyan3', 'steelblue', 'lightcoral', 'grey1', 'purple', 'blue',
                              'rosybrown', 'limegreen', 'red', 'darkgoldenrod', 'orange',  'darkgreen',
                              'grey65')) +
  ggtitle("Denmark") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(  alpha = "none", color = "none",
           size ="none")  +
  labs(x = "", y = "") +
  theme_classic() +
  theme(  plot.background = element_rect(fill = "white", color = NA), 
          panel.background = element_rect(fill = "white", color = "black"))  +
  xlim(1970, 2024) +
  ylim(-0.65, 0.55)


# Arrange plot and both legends
P_DK_l1 <- cowplot::plot_grid(p_DK_nol, legend2, ncol = 2, 
                              rel_widths = c(0.8, 0.2) )
P_DK_l12 <- cowplot::plot_grid(P_DK_l1, legend1, ncol = 1, 
                               rel_heights = c(0.9, 0.1)) 



## arrange in 3 graphs: max 3 rows, 3 columns

ggarrange(P_US_l12, P_UK_l12, P_CA_l12,P_AUS_l12, P_NZ_l12, 
          ncol = 2, nrow=3, align = "hv" ) +
  theme(plot.margin = margin(0.2,0.1,0.2,0.1, "cm"))
# Then save as SVG, size 900x1000


ggarrange(P_NO_l12, P_SE_l12, P_DK_l12,P_GE_l12, P_NL_l12, 
          ncol = 2, nrow=3, align = "hv" ) +
  theme(plot.margin = margin(0.2,0.1,0.2,0.1, "cm"))
# Then save as SVG, size 900x1000


ggarrange(P_CH_l12, P_AT_l12,  P_FR_l12, P_ES_l12,P_IT_l12,  
          ncol = 2, nrow=3, align = "hv" ) +
  theme(plot.margin = margin(0.2,0.1,0.2,0.1, "cm"))
# Then save as SVG, size 900x1000
############### APPENDICES

### United Kingdom: long-term trend

data_UK60 <- data %>% subset((country_code == 'UK' | country_code=='UK60') & (party_abbreviation=='con' | party_abbreviation=='green'
                                                   | party_abbreviation=='lab' | party_abbreviation=='lib' 
                                                   | party_abbreviation=='ukip' ))

table(data_UK60$party_abbreviation, data_UK60$year)
data_UK60 <- data_UK60 %>% mutate(party_abbreviation = ifelse(party_abbreviation=='green', 'gr', party_abbreviation) )

p_UK60 <- data_UK60 %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_UK60,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=c(0,8), breaks=mybreaks) +
  scale_color_manual(values=c('blue', 'springgreen4', 'red', 'orange','grey1')) +
  ggtitle("United Kingdom (incl. '55-'70)") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6),  ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1950, 2022) +
  ylim(-0.65, 0.55)

#Figure A2
plot(p_UK60)


############## COMPARISONS PRESIDENTIAL AND PARLIAMENTARY


############ FRANCE

install.packages("ggrepel")
library('ggrepel')
### France

# directed ur-pol scores (top4/bottom4)
data_FR_pres <- data %>% subset((country_code == 'FR_pres' | country_code== 'FR_pres2'))

p_FR_pres <- data_FR_pres %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = cand_name, color=country_code, label=cand_name)) +
  geom_point(shape=20, data=data_FR_pres,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  scale_color_manual(values=c('springgreen4', 'red' )) +
  ggtitle("France (pres)") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1990, 2022) +
  ylim(-0.65, 0.35)

plot(p_FR_pres)

# subset 
data_FR_pres_sub <- data_FR_pres %>% subset(data_FR_pres$nat_voteshare>.15 & (data_FR_pres$year==1995 | data_FR_pres$year==2022))

p_FR_pres + geom_text_repel(data=data_FR_pres_sub, label=data_FR_pres_sub$cand_name)


### COMPARE FIRST ROUND PRES AND PARL

# directed ur-pol scores (top4/bottom4)
data_FR_pres1 <- data %>% subset(country_code == 'FR_pres')

p_FR_pres1 <- data_FR_pres1 %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = cand_name, label=cand_name)) +
  geom_point(shape=20, data=data_FR_pres,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  ggtitle("President") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1990, 2022) +
  ylim(-0.65, 0.55)


# subset 
data_FR_pres1_sub <- data_FR_pres1 %>% subset(data_FR_pres1$nat_voteshare>.15 ) #& (data_FR_pres$year==1995 | data_FR_pres$year==2022)

p_FR_pres1_text <- p_FR_pres1 + geom_text_repel(data=data_FR_pres1_sub, label=data_FR_pres1_sub$cand_name, 
                                                aes(size=0.10))



## parliamentary 

### France
data_FR <- data %>% subset(country_code == 'FR' & (party_abbreviation=='COM' 
                                                   | party_abbreviation=='ECO' | party_abbreviation=='REC' 
                                                   | party_abbreviation=='SOC' | party_abbreviation=='RN'
                                                   | party_abbreviation=='LR' | party_abbreviation=='DVD'
                                                   | party_abbreviation=='LREM/ENS' | party_abbreviation=='NUP'
                                                   | party_abbreviation=='EXG' | party_abbreviation=='DVG')) 


data_FR <- data_FR %>% mutate(party_abbreviation = ifelse(party_abbreviation=='LREM/ENS', 'LREM', party_abbreviation) )

table(data_FR$party_abbreviation, data_FR$year)

p_FR <- data_FR %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_FR,  aes(size=nat_voteshare, alpha = 0.75)) +
  scale_size(name="Vote share",range=c(0,8), breaks=mybreaks) +
  scale_color_manual(values=c('red', 'cyan1', 'purple', 'limegreen', 'steelblue', 'orange', 'gold',
                              'springgreen4', 'blue', 'grey1', 'lightcoral' )) +
  ggtitle("France: Parliament") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none",  alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = c(0.3, 0.1), legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1990, 2022) +
  ylim(-0.65, 0.55)

######## AUSTRIA 


## parliamentary 

data_AT <- data %>% subset(country_code == 'AT' & 
                             (party_abbreviation == 'BZÖ' | party_abbreviation == 'FPÖ' |
                                party_abbreviation == 'GRÜNE' | party_abbreviation == 'NEOS' |
                                party_abbreviation == 'SPÖ' | party_abbreviation == 'ÖVP'| 
                                party_abbreviation == 'LIF' ))
p_AT <- data_AT %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = party_abbreviation, color=party_abbreviation)) +
  geom_line(linetype="dotted" ) +
  geom_point(shape=20, data=data_AT,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  scale_color_manual(values=c('purple', 'grey1', 'limegreen', 'orange','rosybrown' , 'blue', 'red')) +
  ggtitle("Austria: Parliament") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = c(0.3, 0.1), legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1990, 2022) +
  ylim(-0.65, 0.55)


# directed ur-pol scores (top4/bottom4)
data_AT_pres1 <- data %>% subset(country_code == 'AT_pres')

p_AT_pres1 <- data_AT_pres1 %>%
  ggplot( aes(x=year, y=ur_pol_dir4, group = cand_name, label=cand_name)) +
  geom_point(shape=20, data=data_AT_pres1,  aes(size=nat_voteshare, alpha=.75)) +
  scale_size(name="Vote share", range=c(0,6), breaks=mybreaks)   +
  ggtitle("President") +
  geom_hline(yintercept = c(0), color = "firebrick", linetype="dashed") +
  guides(size = "none", alpha = "none", color = guide_legend(override.aes = list(size = 6), ncol=4)) +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "bottom", legend.title = element_blank(),
        legend.spacing.x = unit(0.01, 'cm'),
        plot.background = element_rect(fill = "white", color = NA), 
        panel.background = element_rect(fill = "white", color = "black"), 
        legend.background = element_rect(fill = "white", color = NA),
        legend.margin=margin(t=-25), plot.margin = unit(c(0,0,0,0), "lines"))  +
  xlim(1990, 2022) +
  ylim(-0.65, 0.55)

# subset 
data_AT_pres1_sub <- data_AT_pres1 %>% subset(data_AT_pres1$nat_voteshare>.1 ) %>%
  mutate(cand_name = ifelse(cand_name=="DrAlexanderVanderBellen", "VanderBellen", cand_name),
         cand_name = ifelse(cand_name=="DrWalterRosenkranz", "Rosenkranz", cand_name))

p_AT_pres1_text <- p_AT_pres1 + geom_text_repel(data=data_AT_pres1_sub, label=data_AT_pres1_sub$cand_name, 
                                                aes(size=0.10))




# Figure A4.2
ggarrange(p_FR, p_FR_pres1_text,
          p_AT, p_AT_pres1_text, ncol = 2, nrow=2 ) +
  theme(plot.margin = margin(1,0.2,0.2,0.2, "cm"))
